<template>
  <div>
    <ZgPage
      ref="page"
      title="系统配置管理"
      addBtnName="新增配置"
      :pageOpt="{showConditionBtn:true,conditionHeight:110}"
      :addOpt="{title:'新增配置'}"
      :updateOpt="{title:'修改配置'}"
      @search="onSearch"
      @del="onDel"
      @add="onAdd"
      @update="onUpdate"
      @searchWithCondition="onSearchWithCondition"
      @resetCondition="onResetCondition"
    >
      <template slot="columns">
        <a-table-column :width="150" dataIndex="name" title="名称"></a-table-column>
        <a-table-column :width="150" dataIndex="code" title="编码"></a-table-column>
        <a-table-column :width="100" title="类型">
          <template slot-scope="text,record">
            <a-tag size="small">{{ getTypeName(record.type) }}</a-tag>
          </template>
        </a-table-column>
        <a-table-column title="值">
          <template slot-scope="text,record">
            {{ record.content }}
          </template>
        </a-table-column>
      </template>
      <template slot="condition">
        <a-row>
          <a-col :span="6">
            <div class="flx pd5">
              <div class="w120 tar">配置名称：</div>
              <ZgInput class="flx-1" v-model="condition.name"></ZgInput>
            </div>
          </a-col>
          <a-col :span="6">
            <div class="flx pd5">
              <div class="w120 tar">配置编码：</div>
              <ZgInput class="flx-1" v-model="condition.code"></ZgInput>
            </div>
          </a-col>
          <a-col :span="6">
            <div class="flx pd5">
              <div class="w120 tar">配置类型：</div>
              <ZgSelect class="flx-1" v-model="condition.type" :options="typeArr"></ZgSelect>
            </div>
          </a-col>
        </a-row>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
import { getTypeName, typeArr } from './type'
export default {
  components: {
    Add, Update
  },
  data () {
    return {
      typeArr,
      condition: {
        name: '',
        code: '',
        type: ''
      }
    }
  },
  methods: {
    getTypeName,
    onSearch (param) {
      this.$http.get('/zg-system/config/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onSearchWithCondition (param) {
      this.$http.get('/zg-system/config/page', Object.assign({}, this.condition, param), data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onResetCondition () {
      this.condition = {
        name: '',
        code: '',
        type: ''
      }
    },
    onDel (ids) {
      this.$http.post('/zg-system/config/del', ids, () => {
        this.$refs.page.search()
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/config/add', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onUpdate () {
      const result = this.$refs.update.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/config/update', result.data, () => {
        this.$refs.page.hideUpdate()
        this.$refs.page.search()
      })
    },
    getKeyValStr (str) {
      const arr = JSON.parse(str)
      return arr.map(t => {
        return t.key
      }).join(',')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
